package com.zhuiyun.project.api.appuser.service.impl;

import com.zhuiyun.project.api.appuser.entity.User;
import com.zhuiyun.project.api.appuser.mapper.AppUserMapper;

import com.zhuiyun.project.api.appuser.model.AppUserModel;
import com.zhuiyun.project.api.appuser.service.AppUserService;
import com.zhuiyun.project.api.sysmenu.entity.SysMenuEntity;
import com.zhuiyun.project.api.wxlogin.mapper.WxLoginMapper;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.util.DateTimeUtils;
import com.zhuiyun.project.util.ObjectUtils;
import com.zhuiyun.project.util.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.util.pageUtil.PageRequest;
import com.zhuiyun.project.util.pageUtil.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName UserServiceImpl
 * @Description TODO
 * @Author hbx
 * @Date 2023/6/5 17:11
 **/
@Service
public class AppUserServiceImpl implements AppUserService {

    @Autowired
    AppUserMapper appuserMapper;
    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;
    @Autowired
    WxLoginMapper wxLoginMapper;



    /**
     * 根据用户名称获取用户信息
     *
     * @return java.util.List<com.zhuiyun.project.api.user.entity.User>
     * @author 温
     * @date 2023/6/5 18:04
     * @parameter [nickName, pageRequest]
     */
    @Override
    public PageResult userList(String userName, PageRequest pageRequest) {
        MybatisPageHelper.startPage(pageRequest);
        List<User> users = appuserMapper.userList(userName, pageRequest);
        return MybatisPageHelper.getPageResult(users);
    }


    /**
     * 根据id 修改禁言状态 未禁言 0 禁言 1
     *
     * @return int
     * @author 温
     * @date 2023/6/6 9:13
     * @parameter [id]
     */
    @Override
    public int logOffList(User appuser) {
        if (appuser.getLogOff().equals("0")) {
            appuser.setLogOff("0");
            return appuserMapper.logOffList(appuser);
        } else if (appuser.getLogOff().equals("1")) {
            appuser.setLogOff("1");
            return appuserMapper.logOffList(appuser);
        } else {
            return 0;
        }

    }


    /**
     * 根据前端传来的数据注销原因来进行注销
     *
     * @return int
     * @author 温
     * @date 2023/6/20 16:35
     * @parameter [id, cancellationReason]
     */
    @Override
    public int useradd(Integer cancellationId, String cancellationReason) {
        int useradd = appuserMapper.useradd(cancellationId, cancellationReason);
        appuserMapper.userUpdate(cancellationId); // 修改注销状态
        return useradd;
    }


//    /**
//     * 彻底删除注销的用户
//     *
//     * @return int
//     * @author 温
//     * @date 2023/6/15 9:15
//     * @parameter [id]
//     */
//    @Override
//    public int userDelete(int id) {
//        int i = appuserMapper.userDelete(id);
//        return i;
//    }

//
//    /**
//     * @param userId
//     * @Author kdj
//     * @Description 根据userId获取user
//     * @Date 2023/6/21 21:30
//     * @Return com.zhuiyun.project.api.appuser.entity.User
//     */
//    @Override
//    public User getUserByUserId(int userId) {
//        Example example = new Example(User.class);
//        example.createCriteria().andEqualTo("id", userId);
//        return appUserMapper.selectOneByExample(example);
//
//    }

    /**
     * @param userId
     * @Author kdj
     * @Description 根据userId获取user
     * @Date 2023/6/21 21:30
     * @Return com.zhuiyun.project.api.appuser.entity.User
     */
    @Override
    public User getUserByUserId(int userId) {
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("id", userId);
        return appuserMapper.selectOneByExample(example);
    }

    /**
     * @param id
     * @Author kdj
     * @Description 根据userId获取头像id
     * @Date 2023/6/27 10:20
     * @Return int
     */
    @Override
    public int getAvatarId(Integer id) {
        Example example = new Example(User.class);
        example.createCriteria().andEqualTo("id", id);
        return appuserMapper.selectOneByExample(example).getImageId();
    }


    /**
     * @Author 小江
     * @Description //TODO 查询用户
     * @Date 19:44 2023/7/13
     **/
    @Override
    public Map userMain(int userId) {
        //创建Example
        Example userExample = new Example(User.class);
        userExample.createCriteria().andEqualTo("id", userId);
        User user = appuserMapper.selectOneByExample(userExample);
        //存入map
        Map map = new HashMap<>();
        map.put("userId", user.getId());
        map.put("imageId", user.getImageId());
        map.put("userName", user.getUserName());
        map.put("userSex", user.getUserSex());
        map.put("userPhone", user.getUserPhone());
        //年龄
        Date userBirthday = user.getUserBirthday();
        Integer age = null;
        if (userBirthday != null) {
            try {
                age = DateTimeUtils.getAge(userBirthday);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        map.put("age", age);
        map.put("prohibition", user.getProhibition() == 1 ? true : false);
        map.put("logOff", Integer.parseInt(user.getLogOff()) == 1 ? true : false);
        return map;
    }

    /**
     * @param toId
     * @Author kdj
     * @Description 好友请求新消息数量
     * @Date 2023/7/21 16:20
     * @Return void
     */
    @Override
    public void addFriendNewNum(int toId) {
        appuserMapper.addFriendNewNum(toId);
    }

    /**
     * @param userId
     * @param num
     * @Author kdj
     * @Description 修改请求好友的未读数
     * @Date 2023/7/21 16:40
     * @Return java.lang.Object
     */
    @Override
    public int upFriendNewNum(int userId, int num) {
        return appuserMapper.upFriendNewNum(userId, num);
    }


    /**
     * @Author 小江
     * @Description //TODO 去重手机号
     * @Date 15:07 2023/8/11
     **/
    @Override
    public CommonResult userTitle(String userPhone) {
        User user = new User();
        user.setUserPhone(userPhone);
        int i = appuserMapper.selectCount(user);
        if (i > 0) {
            return CommonResult.error(i);
        } else {
            return CommonResult.ok(i);
        }
    }

    /**
     * @param userId
     * @Author kdj
     * @Description 用户注销
     * @Date 2023/8/14 16:04
     * @Return int
     */
    @Override
    public int toProhibition(int userId) {
        return appuserMapper.toProhibition(userId);
    }

    @Override
    public int openProhibition(int userId) {
        return appuserMapper.openProhibition(userId);
    }

    /**
     * @param userId
     * @Author kdj
     * @Description 根据手机号查询账号注销状态
     * @Date 2023/8/14 16:04
     * @Return int
     */
    @Override
    public CommonResult selectUserProhibition(String userPhone) {
        AppUserModel userInfoByPhone1 = appuserMapper.getUserInfoByPhone1(userPhone);
        if (userInfoByPhone1==null) {
            return CommonResult.ok(0);
        }
        return CommonResult.ok(appuserMapper.selectUserProhibition(userPhone));
    }


}
